package com.cus.shd.sharingjdbc.switchDataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.activation.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ASUS
 * @Description TODO
 * @Date 2021/11/7 16:22
 **/
@Configuration
public class MutilDataSourceConfiguration {

    @Primary//不加这个会报错。
    @Bean(name = "multiDataSource")
    @DependsOn({"druidDataSource","shardingSphereDataSource"})
    public DataSourceContextHolder multipleDataSource(DruidDataSource dataSource,ShardingSphereDataSource shardingSphereDataSource) {
        DataSourceContextHolder dynamicDataSource = new DataSourceContextHolder();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceEnum.DRUILD.toString(), dataSource);
        targetDataSources.put(DataSourceEnum.SHARDING.toString(), shardingSphereDataSource);
        dynamicDataSource.setTargetDataSources(targetDataSources);
        dynamicDataSource.setDefaultTargetDataSource(dataSource); // 默认使用的数据源
        return dynamicDataSource;
    }

//    @Bean("sqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactory(DataSourceContextHolder dataSource) throws Exception {
//        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
//        sqlSessionFactory.setDataSource(dataSource);
//        //mybatis-plus yml 配置不生效，要在这里代码里配置
//        MybatisConfiguration configuration = new MybatisConfiguration();
//        configuration.setJdbcTypeForNull(JdbcType.NULL);
//        //是否使用转驼峰
//        configuration.setMapUnderscoreToCamelCase(false);
//        configuration.setCacheEnabled(false);
//        sqlSessionFactory.setConfiguration(configuration);
//
//        //添加分页功能
//        Interceptor[] plugins = {paginationInterceptor()};
//        sqlSessionFactory.setPlugins(plugins);
//
//        //扫描 mapper 路径
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        Resource[] resource = resolver.getResources("classpath:mappers/**/*.xml");
//        sqlSessionFactory.setMapperLocations(resource);
//        return sqlSessionFactory.getObject();
//    }


    /**
     * @Description : mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
        paginationInterceptor.setOverflow(true);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
//        paginationInterceptor.setLimit(30);
        return paginationInterceptor;
    }




}
